//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System.Collections.Generic;
using System.Configuration;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using EntLibContrib.Resource.Configuration;
using EntLibContrib.Resource.Configuration.Tests;
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Resource.Providers.Tests
{
	[TestClass]
	public class CustomResourceProviderFixture
	{
		private CustomResourceProviderData customData;
		private ResourceManagerSettings settings;
		private const string providerName = "custom";
		private const string resourceBaseName = "customBaseName";
		private const string attributeValue = "customValue";

		[TestInitialize]
		public void InitializeTests ()
		{
			customData = new CustomResourceProviderData(providerName, resourceBaseName, typeof(MockCustomResourceProvider));
			customData.SetAttributeValue(MockCustomProviderBase.AttributeKey, attributeValue);
			settings = new ResourceManagerSettings();
			settings.ResourceProviders.Add(customData);
			settings.ResourceManagers.Add(new ResourceManagerData("ignore", providerName));
		}

		[TestMethod]
		public void CanBuildCustomResourceProviderFromGivenConfiguration()
		{
			DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();
			configurationSource.Add(ResourceManagerSettings.SectionName, settings);

			IResourceProvider custom = EnterpriseLibraryFactory.BuildUp<IResourceProvider>(providerName, configurationSource);

			Assert.IsNotNull(custom);
			Assert.AreSame(typeof(MockCustomResourceProvider), custom.GetType());
			Assert.AreEqual(attributeValue, ((MockCustomResourceProvider)custom).customValue);
			Assert.AreEqual(resourceBaseName, ((MockCustomResourceProvider)custom).ResourceBaseName);
		}

		[TestMethod]
		public void CanBuildCustomResourceProviderFromSavedConfiguration()
		{
			IDictionary<string, ConfigurationSection> sections = new Dictionary<string, ConfigurationSection>(1);
			sections[ResourceManagerSettings.SectionName] = settings;
			IConfigurationSource configurationSource = ConfigurationTestHelper.SaveSectionsInFileAndReturnConfigurationSource(sections);

			IResourceProvider custom = EnterpriseLibraryFactory.BuildUp<IResourceProvider>(providerName, configurationSource);

			Assert.IsNotNull(custom);
			Assert.AreSame(typeof(MockCustomResourceProvider), custom.GetType());
			Assert.AreEqual(attributeValue, ((MockCustomResourceProvider)custom).customValue);
			Assert.AreEqual(resourceBaseName, ((MockCustomResourceProvider)custom).ResourceBaseName);
		}

		[TestMethod]
		public void CanDeserializeSerializedConfiguration()
		{
			IDictionary<string, ConfigurationSection> sections = new Dictionary<string, ConfigurationSection>(1);
			sections[ResourceManagerSettings.SectionName] = settings;
			IConfigurationSource configurationSource = ConfigurationTestHelper.SaveSectionsInFileAndReturnConfigurationSource(sections);

			ResourceManagerSettings sourceSettings = (ResourceManagerSettings)configurationSource.GetSection(ResourceManagerSettings.SectionName);

			Assert.IsNotNull(sourceSettings);
			Assert.AreEqual(1, sourceSettings.ResourceProviders.Count);

			Assert.IsNotNull(sourceSettings.ResourceProviders.Get(providerName));
			Assert.AreSame(typeof(CustomResourceProviderData), sourceSettings.ResourceProviders.Get(providerName).GetType());
			Assert.AreEqual(providerName, ((CustomResourceProviderData)sourceSettings.ResourceProviders.Get(providerName)).Name);
			Assert.AreEqual(typeof(MockCustomResourceProvider), ((CustomResourceProviderData)sourceSettings.ResourceProviders.Get(providerName)).Type);
			Assert.AreEqual(attributeValue, ((CustomResourceProviderData)sourceSettings.ResourceProviders.Get(providerName)).Attributes[MockCustomProviderBase.AttributeKey]);
			Assert.AreEqual(resourceBaseName, ((CustomResourceProviderData)sourceSettings.ResourceProviders.Get(providerName)).Attributes[ResourceProviderData.ResourceBaseNameProperty]);
		}
	}
}
